package gui_classes;

import java.awt.*;

import java_persistence.AppointmentMapper;
import java_persistence.TreatmentRecordMapper;

import javax.swing.*;
import java.awt.event.*;

import domain.*;
import persistence.*;

import java.util.Iterator;
import java.util.List;

public class ViewingPatientPanel extends JPanel {
	
	private JComboBox comboBox;
	private Patient patient;
	private DERPBase callingPage;
	

	/**
	 * Create the panel.
	 */
	public ViewingPatientPanel(Patient pat, DERPBase callingPage) {
		
		setLayout(null); 
		setPreferredSize(new Dimension(300, 300));
		
		this.patient  = pat;
		this.callingPage = callingPage;
		
		JLabel lblViewMedicalHistory = new JLabel("View Medical History/ Appointments");
		lblViewMedicalHistory.setBounds(24, 12, 276, 26);
		add(lblViewMedicalHistory);
		
		
	//	List<String> apps = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_appointment_dates(this.pat.getUsername());
		List<String> apps = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_appointment_dates(pat.getUsername());
		int i=0;
		String[] appStr = new String[apps.size()];
			for ( Iterator<String> appsIter = apps.iterator(); appsIter.hasNext(); ) {
				String cura = appsIter.next();
				appStr[i]=cura;
				i++;
			}
		
		
		comboBox = new JComboBox(appStr);
		comboBox.setBounds(74, 41, 147, 26);
		add(comboBox);
		
		JButton btnView = new JButton("View");
		btnView.setBounds(86, 92, 117, 25);
		btnView.addActionListener(new viewListener());
		add(btnView);
		
/*		JButton btnCreateRecord = new JButton("Create Record");
		btnCreateRecord.setBounds(74, 96, 154, 25);
		btnCreateRecord.addActionListener(new createRecordListener());
		add(btnCreateRecord); */
		
		if(this.callingPage.getClass().isAssignableFrom(DoctorPage.class))    
		{
			JButton btnGenerateInvoice = new JButton("Generate Patient Report");
			btnGenerateInvoice.setBounds(29, 147, 235, 25);
			btnGenerateInvoice.addActionListener(new generateReportListener());
			add(btnGenerateInvoice);
		}
		
	/*	JButton btnEditPatient = new JButton("Edit Patient");
		btnEditPatient.setBounds(86, 222, 117, 25);
		add(btnEditPatient); */
		
	}
	
	public Patient getPat()
	{
		return this.patient;
	}
	
	public JComboBox getComboBox()
	{
		return this.comboBox;
	}
	
	private class viewListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			
		//	GUI_Main.getSession().getMapper(AppointmentMapper.class).g
			
			String date = (String) GUI_Main.viewpinfo.getViewingPanel().getComboBox().getSelectedItem();   // this is the date of the appointment
			
			//if the appointment is a history/tr .. check diagnosis
			String patient_username = GUI_Main.viewpinfo.getViewingPanel().getPat().getUsername();
			
			String complaint = GUI_Main.getSession().getMapper(AppointmentMapper.class).get_complaint(date, patient_username);
			
			String[] dates = date.split(" ");
			String day = dates[0];
			String time = dates[1];
			GUI_Main.tscreen = new TreatmentScreen(GUI_Main.viewpinfo.getPatient(), callingPage);
			GUI_Main.tscreen.setPrevious(GUI_Main.viewpinfo);
			GUI_Main.tscreen.setTextArea(complaint);
			GUI_Main.tscreen.setDate(day);
			GUI_Main.tscreen.setTime(time);
			
			// null when just appointment
			String diagnosis = GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_diagnosis(date, patient_username);
			if(diagnosis!=null) // 
			{
				GUI_Main.tscreen.setDiagnosis(GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_diagnosis(date, patient_username));
				GUI_Main.tscreen.setBPD(GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_bp_diastolic(date, patient_username).toString());
				GUI_Main.tscreen.setBPS(GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_bp_systolic(date, patient_username).toString());
				GUI_Main.tscreen.setHt(GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_height_inches(date, patient_username).toString());
				GUI_Main.tscreen.setTemp(GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_temp(date, patient_username).toString());
				GUI_Main.tscreen.setWt(GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_weight_pounds(date, patient_username).toString());
			//	GUI_Main.tscreen.setPulse(GUI_Main.getSession().getMapper(TreatmentRecordMapper.class).get_pulse(date, patient_username).toString());



			}
			GUI_Main.tscreen.display();
		}
	}
	private class createRecordListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			// do a treatment screen method
			GUI_Main.tscreen = new TreatmentScreen(GUI_Main.viewpinfo.getPatient(), callingPage);
			GUI_Main.tscreen.setPrevious(GUI_Main.viewpinfo);
			GUI_Main.tscreen.display();
		}
	}
	
	private class generateReportListener implements ActionListener{
		public void actionPerformed(ActionEvent event)
		{
			Reports current = new Reports(GUI_Main.getSession());
			current.patientChart(patient.getUsername());
			
			
			GUI_Main.pchartpage = new PatientChartPage(current.getPanel());
			GUI_Main.pchartpage.setPrevious(GUI_Main.viewpinfo);
			GUI_Main.pchartpage.display();
		}
	}
}
